package com.hoopchina.ui.main;

import com.hoopchina.news.R;
import com.hoopchina.ui.home.HomeView;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * TitleBar
 * main activity的标题栏
 * 
 * @author Neusoft
 */
public class TitleBar extends UIWidget {
	private TextView              mTxtTitle;
	private TextView              mTxtStatus;
//	private Button                mBtnPrimary;
//	private Button                mBtnBack;
	private Button                mBtnAssist;
	private Button                mBtnSubscriptiontitlebar;
	private Button                mBtnGraphicSelect;
	private Button                mBtnCategoryRead;
    private WeatherBox mWeatherBox;
    private ProgressBar mLoadingBar;
    private Button mDetailsNextBar;
    private Button mDetailsPreBar;

	/*
	 *  构造器
	 */
	public TitleBar(Context context) {
		super(context);
	}

	/*
	 * 构造器
	 */
	public TitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	/**
	 * 加载相关资源
	 */
	@Override
	protected void loadResources() {
		LayoutInflater inflater = 
			( LayoutInflater )getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
		inflater.inflate( R.layout.titlebar, this, true );
		
		mTxtTitle = ( TextView )findViewById( R.id.txtTitleBarTitle );
		mTxtStatus = ( TextView )findViewById( R.id.txtTitleBarStatus );
//		mBtnPrimary = ( Button )findViewById( R.id.btnTitleBarPrimary );
//		mBtnBack = ( Button )findViewById( R.id.btnTitleBarBack );
//		mBtnBack.setBackgroundResource( R.drawable.button_back );
		mBtnAssist = ( Button )findViewById( R.id.btnTitleBarAssist );
		mWeatherBox = ( WeatherBox )findViewById( R.id.weatherBox );
		mLoadingBar = (ProgressBar)findViewById(R.id.loadProgressBar);
	    mDetailsNextBar = ( Button )findViewById( R.id.nextbutton );
        mDetailsPreBar = ( Button )findViewById( R.id.previousbutton );
		mBtnSubscriptiontitlebar = ( Button )findViewById(R.id.btnTitleBarSubscribe);
		mBtnGraphicSelect = ( Button )findViewById(R.id.btnGraphicSelect);
		mBtnCategoryRead = ( Button )findViewById(R.id.btnCategoryRead);
	}
	
	/**
	 * 设置标题
	 * @param aTitle 标题字符串
	 */
	public void setTitle( String aTitle ) {
		mTxtTitle.setText( aTitle );
	}
	
	public void setTitle( int aTitle ) {
		mTxtTitle.setText( aTitle );
	}
	/**
	 * 设置状态栏
	 * @param aStatus 状态栏文字
	 */
	public void setStatus( String aStatus ) {
		mTxtStatus.setText( aStatus );
		mBtnAssist.setVisibility( View.INVISIBLE );
		mTxtStatus.setVisibility( View.VISIBLE );
	}
	
	/**
	 * Set title bar back button
	 * @param aVisible if button will be visible
	 * @param aListener the button click listener
	 */
	public void setBackButton( String aText, OnClickListener aListener ) {
		// relative layout, so invisible is enough, not bother to set them as gone
//		mBtnBack.setOnClickListener( aListener );
//		mBtnBack.setText( aText );
//		mBtnBack.setVisibility( View.VISIBLE  );
//		mBtnPrimary.setVisibility( View.INVISIBLE );
	}
	
	/**
	 * Set title bar primary button
	 * @param aVisible if button will be visible
	 * @param aListener the button click listener
	 */
	public void setPrimaryButton( OnClickListener aListener ) {
		// relative layout, so invisible is enough, not bother to set them as gone
//		mBtnPrimary.setOnClickListener( aListener );
//		mBtnBack.setVisibility( View.VISIBLE );
//		mBtnBack.setVisibility( View.INVISIBLE );
	}
	
	/**
	 * Hide left buttons
	 */
	public void hideLeftButton() {
//		mBtnBack.setVisibility( View.INVISIBLE );
//		mBtnPrimary.setVisibility( View.INVISIBLE );
	}
	/**
	 * 隐藏天气框
	 */
    public void displayWeatherBox(boolean isDisp) {
        if ( isDisp ) {
            mWeatherBox.setVisibility( WeatherBox.VISIBLE );
        }
        else {
            mWeatherBox.setVisibility( WeatherBox.GONE );
        }
    }
    /**
	 * 刷新天气框
	 */
    public void refreshWeatherBoxCity() {
    	mWeatherBox.refresh();
    }
    /**
     * 城市选择
     * @param resultNum
     */
    public void citySelect(int resultNum,ILocalNewsChanged localNewsChanged) {
    	mWeatherBox.citySelect(resultNum,localNewsChanged);
    }
    
	/**
	 * 设置 assist button
	 * @param aText 该按钮文本
	 * @param aListener 该按钮监视器
	 */
	public void setAssistButton( String aText, int aBgResource, OnClickListener aListener ) {
		mBtnAssist.setOnClickListener( aListener );
		if ( aBgResource == 0 ) {
			mBtnAssist.setText( aText );
			mBtnAssist.setBackgroundResource( aText.length() > 2
					? R.drawable.button_4chars
				    : R.drawable.button_2chars );
		}
		else {
			mBtnAssist.setText( "" );
			mBtnAssist.setBackgroundResource( aBgResource );
		}
		mTxtStatus.setText( "" );
		mTxtStatus.setVisibility( View.INVISIBLE );
		mBtnAssist.setVisibility( View.VISIBLE );
	}
	/*
	 * 是否显示辅助按钮
	 */
	public void showAssistButton( boolean isshow ){
		if( isshow == true )
			mBtnAssist.setVisibility( View.VISIBLE );
			else
			mBtnAssist.setVisibility( View.GONE );
	}
	
	/*
	 * 是否显示加载进度条
	 */
	public void showLoadingProgressBar( boolean isshow ){
		if( isshow == true )
		mLoadingBar.setVisibility( View.VISIBLE );
		else
		mLoadingBar.setVisibility( View.GONE );
	}
	
	public void showNextButton( boolean isshow ){
		if( isshow == true )
		mDetailsNextBar.setVisibility( View.VISIBLE );
		else
		mDetailsNextBar.setVisibility( View.INVISIBLE );
	}
	
	public void showPreButton( boolean isshow ){
		if( isshow == true )
		mDetailsPreBar.setVisibility( View.VISIBLE );
		else
		mDetailsPreBar.setVisibility( View.INVISIBLE );
	}
	
	public void showDetailViewBar( boolean isshow ){
		if( isshow == true )
		{
		    mDetailsNextBar.setVisibility( View.VISIBLE ); 
	        mDetailsPreBar.setVisibility( View.VISIBLE );
		}
			else
			{
				mDetailsNextBar.setVisibility( View.GONE );
				mDetailsPreBar.setVisibility( View.GONE );
			}
		}
	
	/**
	 * 在新闻详细里设置下一篇按钮监听器
	 * @param aListener 按钮点击监听器
	 */
	public void setNextButton( OnClickListener aListener ) {
		mDetailsNextBar.setOnClickListener( aListener );
	}
	
	/**
	 * 在新闻详细里设置上一篇按钮监听器
	 * @param aListener 按钮点击监听器
	 */
	public void setPreButton( OnClickListener aListener ) {
		mDetailsPreBar.setOnClickListener( aListener );
	}
	
	public void showSubscriptionBtn(boolean isshow ){
		if( isshow == true )
			mBtnSubscriptiontitlebar.setVisibility( View.VISIBLE );
		else
			mBtnSubscriptiontitlebar.setVisibility( View.GONE );
	}
	
	public void showGraphicSelectBtn(boolean isshow ){
		if( isshow == true )
			mBtnGraphicSelect.setVisibility( View.VISIBLE );
		else
			mBtnGraphicSelect.setVisibility( View.GONE );
	}
	
	public void showCategoryReadBtn(boolean isshow){
		if( isshow == true )
			mBtnCategoryRead.setVisibility( View.VISIBLE );
		else
			mBtnCategoryRead.setVisibility( View.GONE );
	}
	
	public void setSubscriptionBtnOnClickerLister(OnClickListener aListener){
		mBtnSubscriptiontitlebar.setOnClickListener(aListener);
	}
	
	public void setGraphicSelectBtnOnClickerLister(OnClickListener aListener){
		mBtnGraphicSelect.setOnClickListener(aListener);
	}
	
	public void setCategoryReadBtnOnClickerLister(OnClickListener aListener){
		mBtnCategoryRead.setOnClickListener(aListener);
	}
}
